package com.cn.main.web;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenyuncong on 2018/6/27.
 */
@RestController
@RequestMapping("test")
public class TestWeb {
//    @Autowired
//    private EhCacheCacheManager ehCacheCacheManager;

    @Autowired
    private CacheManager cacheManager;

    @RequestMapping("getVal")
    public Map<String,Object> getVal(String key){
        Map<String,Object> map = new HashMap<>();
        Cache cache=cacheManager.getCache("a");
        Element element = cache.get(key);
        cache.flush();
        if(element!=null){
            map.put(key,element.getObjectValue());
        }

        return map;
    }

    @RequestMapping("setCache")
    public String setCache(String key,String val){
        Cache cache=cacheManager.getCache("a");
        long timeStart = System.currentTimeMillis();
//        for(int i=0;i<10000;i++){
            cache.put(new Element(key, val));
            cache.flush();
//        }
        System.out.println(System.currentTimeMillis()-timeStart);
//        CacheManager.ALL_CACHE_MANAGERS.get(0);
        return "OK";
    }

    @RequestMapping("remove")
    public String remove(String key){
        Cache cache=cacheManager.getCache("a");
        cache.remove(key);
        return "OK";
    }
}
